<?php
namespace app\plugin\controller;
use app\common\controller\Base;
use upload\Upload;
use think\Db;
class Lupload extends Base{

    public function index(){
        if(!$this->privilege['create']&&!$this->privilege['update'])
        {exit(encode(['code'=>0,'msg'=>'暂无权限！']));}
        if(isset($_POST['chunks'])){$total = $_POST['chunks'];} else{$total = 1;}
        if(isset($_POST['chunk'])){$num  = $_POST['chunk'];} else{$num = 0;}
        $num = $num + 1;$name  = $_POST['name'];$name = explode('.',$name);$type = '';
        foreach ($name as $v){
            $type = $v;
        }
        $_filename = $_POST['md5'] .'.'. $type;
        $upload = new Upload($_FILES['file']['tmp_name'],$num,$total,$_filename);
        $res = $upload->apiReturn();
        if($res['code']==2&&!isset($res['status'])){
            $request = $this->request;
            $upload_ip = $request->ip();
            $uploader = $this->admin['admin_account'];
            $size = $_POST['size'];
            $url = $res['file_path'];
            $md5 = $_POST['md5'];
            $filename = $_POST['name'];
            $accept = $_POST['accept'];
            $data = ['url'=>$url,'accept'=>$accept,'filename'=>$filename,'md5'=>$md5,'size'=>$size,'uploader'=>$uploader,'upload_ip'=>$upload_ip];
            Db::connect('db_log')->name('upload_log')->insert($data);
        }
        exit( encode($res) );
    }


}

